package com.manage.service;

import java.util.List;

import org.apache.log4j.Logger;

import com.manage.config.Config;
import com.manage.model.SpInfo;
import com.service.AbstractService;
import com.util.ConfigUtil;
import com.utils.dbcp.ConnectionManager;
import com.utils.dbcp.ModelGenerator;

public class SpInfoServiceImpl  extends AbstractService implements SpInfoService {

	private Logger logger;
	public SpInfoServiceImpl()
	{
		logger = Logger.getLogger(this.getClass());
		this.dbcp = ConfigUtil.get(Config.SECTION, Config.KEY_DBCP_NAME);
	}

	@Override
	public SpInfo getSpInfoById(int id) {
		String sql="select * from spinfo where id="+id;
		ModelGenerator<SpInfo> dbcpmg = new ModelGenerator<SpInfo>(SpInfo.class);
		return dbcpmg.generate(dbcp, sql);
	}

	@Override
	public List<SpInfo> getSpInfos() {
		String sql="select * from spinfo order by createdate desc";
		ModelGenerator<SpInfo> dbcpmg = new ModelGenerator<SpInfo>(SpInfo.class);
		return dbcpmg.generateList(dbcp, sql);
	}

	@Override
	public boolean saveSpInfo(SpInfo spInfo,String type) {
		String sql ="";
		if(type.equalsIgnoreCase("insert"))
		{
			spInfo.setId(getTableId("spinfo"));
			sql = "insert into spinfo(id,spname,spremark,bd,createuser,createdate) values (" +
					""+spInfo.getId()+",'"+spInfo.getSpName()+
					"','"+spInfo.getSpRemark()+"','"+spInfo.getBd()+"','"+spInfo.getCreateUser()+"','"+spInfo.getCreateDate()+"')";
		}else if (type.equalsIgnoreCase("update"))
		{
			sql="update spinfo set spname='"+spInfo.getSpName()+"',bd='"+spInfo.getBd()+"',spremark='"+spInfo.getSpRemark()+"' where " +
					"id ="+spInfo.getId();
		}
		else if (type.equalsIgnoreCase("delete"))
		{
			sql="delete from spinfo where " +
			"id ="+spInfo.getId();
		}
		return ConnectionManager.getInstance().execute(dbcp, sql);
	}


}
